<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016 杭州博也网络科技, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace app\admin\controller;

use app\weixin\Logic\WeixinLogic;
use app\weixin\Logic\WxaccountLogic;
use app\src\admin\helper\DateHelper;
use app\src\wallet\logic\WalletApplyLogic;
use app\src\wallet\logic\WalletLogic;
use app\src\wallet\logic\WalletOrderLogicV2;
use app\src\wallet\model\WalletApply;
use app\src\user\logic\MemberLogic;
use app\src\base\logic\BaseLogic;
use app\src\wxmenu\logic\WxmenuLogic;

class Wxmenu extends Admin{
    public function test() {
        $json = array('button' => array( array('type' => "click")));
        echo json_encode($json);
    }

    public function index() {

        $map = array('wxaccount_id' => 1);
        $page = array('curpage' => input('p', 0), 'size' => cache('LIST_ROWS'));
        $order = " sort desc ";
        //
        //$result = apiCall(WxmenuApi::QUERY, array($map, $page, $order));

        $result=(new WxmenuLogic())->queryWithPagingHtml($map,$page,$order);

        if ($result['status']) {
            $this -> assign("show", $result['info']['show']);
            $this -> assign("list", $result['info']['list']);
            return $this->boye_display();
        } else {
            LogRecord($result['info'], __FILE__);
            $this -> error($result['info']);
        }
    }

    public function add() {
        if (IS_GET) {
            //获取主菜单 $order = false, $fields
            $map = array('wxaccount_id' => getWxAccountID(), 'pid' => 0);


            $result = (new WxmenuLogic())->queryNoPaging(['wxaccount_id'=>1,'pid'=>0], "sort desc", "name,id");
            if ($result['status']) {
                $this -> assign("mainmenus", $result['info']);
                return $this -> boye_display();
            } else {
                LogRecord($result['info'], __FILE__);
                $this -> error($result['info']);
            }

        } elseif (IS_POST) {
            $entity = array('name' => input('name', ''), //菜单名称
                            'menukey' => input('key', ''), 'url' => input('url', ''), 'sort' => input('sort', '1'), 'wxaccount_id' => getWxAccountID(), 'pid' => input('pid', 0), );
            if (!empty($entity['url'])) {
                $entity['type'] = "view";
            } else {
                $entity['type'] = 'click';
            }


            $result = (new WxmenuLogic())->add($entity,'');
            if ($result['status']) {
                $this -> success(L('RESULT_SUCCESS'), url('Admin/Wxmenu/index'));
            } else {
                LogRecord($result['info'], __FILE__);
                $this -> error($result['info']);
            }
        }
    }

    public function edit() {
        if (IS_GET) {

            $id = input('id', 0);

            $result = (new WxmenuLogic())->getInfo(['id'=>$id]);

            if ($result['status']) {
                $this -> assign("menuVO", $result['info']);
                $this -> assign("pid", $result['info']['pid']);
            }

            //获取主菜单 $order = false, $fields
            $map = array('wxaccount_id' => getWxAccountID(), 'pid' => 0);


            $result = (new WxmenuLogic())->queryNoPaging($map,"sort desc", "name,id");
            if ($result['status']) {
                $this -> assign("mainmenus", $result['info']);
                return $this ->boye_display();
            } else {

                LogRecord($result['info'], __FILE__);
                $this -> error($result['info']);
            }

        } elseif (IS_POST) {
            $id = input('id', 0);
            $entity = array('name' => input('name', ''), //菜单名称
                            'menukey' => input('key', ''), 'url' => input('url', ''), 'sort' => input('sort', '1'), 'pid' => input('pid', 0), );
            if (!empty($entity['url'])) {
                $entity['type'] = "view";
            } else {
                $entity['type'] = 'click';
            }

            $result = (new WxmenuLogic())->saveByID($id,$entity);
            if ($result['status']) {
                $this -> success(L('RESULT_SUCCESS'), url('Admin/Wxmenu/index'));
            } else {
                LogRecord($result['info'], __FILE__);
                $this -> error($result['info']);
            }
        }
    }


    public function deleteMenu() {
        if (IS_POST) {

            //$result = apiCall(WxaccountApi::GET_INFO, array( array('id' => getWxAccountID())));
            $result = (new \app\src\wxpay\logic\WxaccountLogic())->getInfo(['id'=>1]);
            if ($result['status']) {
                $appid = $result['info']['appid'];
                $appsecret = $result['info']['appsecret'];

                //$weixinApi = new \Common\Api\WeixinApi($appid, $appsecret);
                $weixinApi = new WeixinLogic($appid, $appsecret);
                $result = $weixinApi -> deleteMenu();
                if ($result['status']) {
                    $this -> success(L("RESULT_SUCCESS"));
                } else {
                    LogRecord($result['msg'], __FILE__);
                    $this -> error($result['msg']);
                }
                //

            } else {
                LogRecord($result['info'], __FILE__);
                $this -> error($result['info']);
            }
        }
    }

    /**
     * 发送到微信服务器
     */
    public function sendToWXServer() {
        if (IS_POST) {
            $map = array('wxaccount_id' => 1);

            $result = (new WxmenuLogic())->queryNoPaging($map,"sort desc", "name,id,pid,menukey,url,type");
            if ($result['status']) {
                $menulist = $result['info'];
                if (count($menulist) > 15) {
                    $this -> error("菜单不能超过15个了！");
                }

                $convertML = $this -> convertMenu($menulist);

                //$result = apiCall(WxaccountApi::GET_INFO, array( array('id' => getWxAccountID())));
                $result = (new \app\src\wxpay\logic\WxaccountLogic())->getInfo(['id'=>1]);
                if ($result['status']) {
                    $appid = $result['info']['appid'];
                    $appsecret = $result['info']['appsecret'];

                    //$weixinApi = new \Common\Api\WeixinApi($appid, $appsecret);
                    $weixinApi = new WeixinLogic($appid, $appsecret);

                    $result = $weixinApi -> createMenu($convertML);
//                    addWeixinLog($result,'菜单测试问题');
//                    if ($result['status']) {
//                        $this -> success(L("RESULT_SUCCESS"));
//                    } else {
//                        LogRecord($result['msg'], __FILE__);
//                        $this -> error($result['msg']);
//                    }
                    //

                } else {
                    LogRecord($result['info'], __FILE__);
                    $this -> error($result['info']);
                }

            } else {
                LogRecord($result['info'], __FILE__);
                $this -> error($result['info']);
            }
        }
    }

    /**
     * 转换为可处理的数据
     */
    private function convertMenu($menulist) {
        $convertMl = array();

        //主菜单
        foreach ($menulist as $key => $menu) {
            if ($menu['pid'] == 0) {
                $convertMl[$menu['id']] = array('name' => $menu['name'], 'type' => $menu['type'], 'url' => $menu['url'], 'key' => $menu['menukey'], '_child' => array());
            }
        }

        //子菜单
        foreach ($menulist as $key => $menu) {
            if ($menu['pid'] > 0 && isset($convertMl[$menu['pid']])) {
                //子菜单
                array_push($convertMl[$menu['pid']]['_child'], array('name' => $menu['name'], 'type' => $menu['type'], 'url' => $menu['url'], 'key' => $menu['menukey'], ));
            }
        }

        return $convertMl;
    }

}
